const app1 = new Vue({
    el: '#app1',
    data: {
        a: [],
        username: 'Candy',
        author: 'author',
        show_header: false,
        is_login: false,
        input4: ""
    },
    created(){
        console.log("初始化")
        token = localStorage.getItem("token")
        this.username = localStorage.getItem("username")
        if (token!=null && token != ""){
            this.is_login = true
        }else{
            this.is_login = false
        }
        fetch('/article/new_list')
        .then(resp => resp.json())
        .then(json => {
            if(json.code==200){
                this.a = json.results.message
                new_array = json.results.message.slice()
                var show_info = shuffleSelf(new_array, new_array.length)[1]
                if(document.getElementById('show-editor')){
                    document.getElementById('show-editor').innerHTML = `<textarea id="message" name="message">` + show_info.content + `</textarea>`
                    document.getElementById('title').innerHTML = show_info.title
                    this.author = show_info.user.name
                    var showEditor
                    $(function() {
                        showEditor = editormd.markdownToHTML("show-editor", {
                            width  : "80%",
                            height : 800,
                            editor: false,
                            watch: true,
                            preview : true,
                            path: "/static/lib/"
                        });
                    });
                }else{
                    console.log(11)
                }
            }
      })
    },
    methods:{
        handleOpen(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        add(row, a_type){
            this.$message(row);
            console.log(row)
            localStorage.setItem('aid', row)
            localStorage.setItem('a_type', a_type)
            location.href = '/static/templates/new_detail.html'
        },
        show(){
            fetch('/article/new_list')
            .then(resp => resp.json())
            .then(json => {
                if(json.code==200){
                    this.a = json.results.message
                    this.$notify({
                        title: '刷新成功',
                        message: '获取最新' + this.a.length + '条数据',
                        type: 'success',
                        offset: 100
                    });
                    return
                }
            })
        },
        login(){
            location.href = '/static/templates/login.html'
        },
        register(){
            location.href = '/static/templates/register.html'
        },
        logout(){
            this.$confirm('此操作将退出登录,您将失去某些操作权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                localStorage.setItem('token', '')
                this.$message({
                    type: 'success',
                    message: this.username + '已退出!'
                });
                setTimeout(function(){
                    location.href = '/'
                },1500)
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        new_editor(){
            location.href = '/static/templates/new_editor.html'
        },
        user_img(){
            this.$message({
                showClose: true,
                message: '此接口暂未开放',
                type: 'warning'
            });
        }
    }
});

function shuffleSelf(array, size) {
    var index = -1,
        length = array.length,
        lastIndex = length - 1;

    size = size === undefined ? length : size;
    while (++index < size) {
        // var rand = baseRandom(index, lastIndex),
        var rand = index + Math.floor( Math.random() * (lastIndex - index + 1))
            value = array[rand];

        array[rand] = array[index];

        array[index] = value;
    }
    array.length = size;
    return array;
}
